package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.YeJingChangJingKongZhiQi0Faa8A03CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;

/**
 * <p>Title:液晶场景控制器 <／p>
 * <p>Description: <／p>
 * <p>Company: JiZi<／p>
 *
 * @author wjming
 * @version 1.0
 * @date 2018年12月10日
 */
public class YeJingChangJingKongZhiQi0Faa8A03 implements Device {

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String cmd = getSceneNumberAndNameSetCmd(map);

        return cmd;
    }


    private String getSceneNumberAndNameSetCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String wayNumber = actionsMap.get("wayNumber");

        String cmd8 = "";
        switch (wayNumber) {
            case "1":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_1;
                break;
            case "2":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_2;
                break;
            case "3":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_3;
                break;
            case "4":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_4;
                break;
            case "5":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_5;
                break;
            case "6":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_6;
                break;
            case "7":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_7;
                break;
            case "8":
                cmd8 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_8_WAY_8;
                break;
            default:
                break;

        }


        String cmd10 = CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("sceneNumber")));

        String cmd11 = CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("sceneNameLength")));

        String cmd12 = "";

        String gb2312SceneName = actionsMap.get("sceneName").toUpperCase();

        if (gb2312SceneName.length() < YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SCENE_NAME_LENGTH) {
            int zeroLength = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SCENE_NAME_LENGTH - gb2312SceneName.length();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < zeroLength; i++) {
                sb = sb.append("0");
            }

            cmd12 = gb2312SceneName + sb.toString();
        } else {
            cmd12 = gb2312SceneName;
        }

        String cmd9 = CmdUtils.getDateLength(cmd10 + cmd11 + cmd12);

        String cmd3To12 = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_3 +
                YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_4 +
                map.get("gatewayMac") +
                map.get("deviceMac") +
                YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_7 +
                cmd8 +
                cmd9 +
                cmd10 +
                cmd11 +
                cmd12;

        String cmd2 = CmdUtils.getDateLength(cmd3To12);

        String cmd13 = CmdUtils.getCmdBodySum(cmd3To12);

        String fullCmd = YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_1 +
                cmd2 +
                cmd3To12 +
                cmd13 +
                YeJingChangJingKongZhiQi0Faa8A03CmdDeal.SceneNumberAndNameSet.CMD_14;

        return fullCmd;
    }


}
